package com.neusoft.nep.admin.controller;

import com.neusoft.nep.admin.entity.Administrator;
import com.neusoft.nep.common.Dialog;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.common.ViewSwitcher;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.io.IOException;
import java.util.Map;

public class AdmiLoginViewController {
    @FXML
    private Button btn_login;
    @FXML
    private TextField txt_logincode;
    @FXML
    private PasswordField txt_password;

    public void login() throws IOException {
        Map<String, Administrator> userMap = FileUtils.readMapObject("administrator.txt");

        String logincode = txt_logincode.getText();
        String password = txt_password.getText();
        System.out.println(logincode);
        System.out.println(password);
        if (userMap.containsKey(logincode)) {
            Administrator u = userMap.get(logincode);
            if (password.equals(u.getPassword())) {
                ViewSwitcher.switchView(btn_login, "../admin/view/AdminIndexView.fxml");
            } else {
                Dialog.showDialog("用户登录", "用户编码不正确");
            }
        } else {
            Dialog.showDialog("用户登录", "用户不存在");
        }
    }
}
